package com.szewec.ps.myview;

import android.text.InputFilter;
import android.text.Spanned;

import java.util.regex.Pattern;

public class NumberInputFilter implements InputFilter {
    private Pattern mPattern;

    public NumberInputFilter(){
        String pattern = "([0-9]{1,3})|([0-9]{1,3}\\.[0-9]{0,3})";
        this.mPattern = Pattern.compile(pattern);
    }

    @Override
    public CharSequence filter(CharSequence charSequence, int start, int end, Spanned spanned, int i2, int i3) {
        if (end > start){
            String des = spanned.toString();
            String result = des.substring(0,i2) + charSequence.subSequence(start,end) + des.substring(i3);
            return result.matches(this.mPattern.toString()) ? null : "";
        }
        return null;
    }
}
